package cn.xiaopengstack.domain.order.service;

import cn.xiaopengstack.domain.order.model.aggregate.CreateOrderAggregate;
import cn.xiaopengstack.domain.order.model.entity.OrderUpdateEntity;
import cn.xiaopengstack.domain.order.model.entity.PayOrderEntity;
import cn.xiaopengstack.domain.order.model.valobj.PayOrderStatusEnumVO;
import cn.xiaopengstack.domain.order.repository.IOrderRepository;
import cn.xiaopengstack.domain.pay.model.entity.CreatePayEntity;
import cn.xiaopengstack.domain.product.adapter.IProductPort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author jiangyangang
 * 订单服务的实现类
 */
@Service
@Slf4j
public class OrderService extends AbsOrderService{


    public OrderService(IOrderRepository orderRepository, IProductPort productPort) {
        super(orderRepository, productPort);
    }

    @Override
    protected void doSaveOrder(CreateOrderAggregate createOrderAggregate){
        orderRepository.createOrder(createOrderAggregate);
    }

    @Override
    public PayOrderEntity prepareToPay(CreatePayEntity createPayEntity) {
        PayOrderEntity payOrderEntity = new PayOrderEntity();
        payOrderEntity.setUserId(createPayEntity.getUserId());
        payOrderEntity.setOrderId(createPayEntity.getOrderId());
        payOrderEntity.setPayUrl(createPayEntity.getPayUrl());
        payOrderEntity.setPayOrderId(createPayEntity.getPayOrderId());
        payOrderEntity.setOrderStatus(PayOrderStatusEnumVO.pay_wait);
        orderRepository.updatePayOrder(payOrderEntity);
        return payOrderEntity;
    }

    @Override
    public void updateOrder(OrderUpdateEntity orderUpdateEntity) {
        // 更新订单
        PayOrderEntity payOrderEntity = PayOrderEntity.builder()
                .payTime(orderUpdateEntity.getPayTime())
                .orderStatus(orderUpdateEntity.getStatusEnum())
                .orderId(orderUpdateEntity.getOrderId())
                .userId(orderUpdateEntity.getUserId())
                .build();
        orderRepository.updatePayOrder(payOrderEntity);
    }
}
